﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using System.Timers;
using Reborn_WorldServer;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_RaiseCorpses : AreaSpell
    {
        private string castingFaction = "";

        public Spell_RaiseCorpses() { }

        public void Cast(CreatureBase myCreatureBase, List<string> MoveList)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, MoveList, out MapItemsWithin12) == false)
                return;

            castingFaction = myCreatureBase.Faction;

            spellTimer.Interval = 2000;

            List<Character> CharactersWithin8 = new List<Character>();
            MapItem CurrentMapItem = null;

            foreach (MapItem mapitem in MapItemsWithin12)
            {
                if (Math.Abs(mapitem.MapX - CastMapX) <= 8 && Math.Abs(mapitem.MapY - CastMapY) <= 8)
                {
                    foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                    {
                        if (creaturebase is Character)
                            CharactersWithin8.Add((Character)creaturebase);
                    }
                }
                if (mapitem.MapX == CastMapX && mapitem.MapY == CastMapY)
                    CurrentMapItem = mapitem;
            }
            CurrentMapItem.SpellLayer0.Add(this);
            MapItems.Add(CurrentMapItem);

            foreach (Character character in CharactersWithin8)
            {
                character.AddMapItem(CurrentMapItem);
                character.PlaySound("RaiseCorpses", CastMapX, CastMapY);
            }

            spellTimer.Start();
        }
        protected override void SpellTimer_Expired(object source, ElapsedEventArgs e)
        {
            TimerCounter = TimerCounter + 1;

            LockItem lockItem = Managers.GameHandler.GetLockItem(MapID, Instance);
            lock (lockItem.LockObj)
            {
                bool foundSpell = false;
                foreach (Spell spell in CastMapItem.SpellLayer0)
                {
                    if (spell.SpellID == SpellID)
                    {
                        foundSpell = true;
                        break;
                    }
                }
                if (foundSpell == false)
                    return;

                Map CurrentMap = Managers.GameHandler.GetMap(MapID, Instance);
                List<Character> CharactersWithin8 = new List<Character>();
                foreach (MapItem mapitem in CastMapItem.mapItemsWithin8)
                {
                    foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                    {
                        if (creaturebase is Character)
                            CharactersWithin8.Add((Character)creaturebase);
                    }
                }
                bool updateMapItem = false;
                bool corpseSpawned = false;
                Corpse corpse = CurrentMap.LootMngr.TakeFirstCreatureCorpse(CastMapX, CastMapY);
                if (corpse != null)
                {
                    CreatureSpawn spawn = new CreatureSpawn(Managers.GameHandler);
                    spawn.MaxHPBase = 500;
                    spawn.HP = 500;

                    spawn.Agility = 5;
                    spawn.Air = false;
                    spawn.Armor = 30;
                    //spawn.CreatureGroupID = creatureGroup.CritGroupID;
                    spawn.CreatureID = Managers.GameHandler.ItemIDGen.GetCreatureUID();
                    spawn.CreatureType = "Creature";
                    spawn.Defense = 5;
                    spawn.Dexterity = 5;
                    spawn.Faction = castingFaction;
                    spawn.ID = -1;
                    spawn.ImageURL = "ReanimatedCorpse";
                    spawn.Instance = Instance;
                    spawn.Intelligence = 5;
                    spawn.Land = true;
                    spawn.MapID = MapID;
                    

                    spawn.MaxMoves = 2;
                    spawn.StartLifespanTimer(90);
                    spawn.RushAttack = true;
                    spawn.Name = "Re-Animated Corpse";
                    spawn.SoundDeath = "MaleScream";
                    spawn.Strength = 5;
                    spawn.WanderMax_E = CastMapX + 4;
                    spawn.WanderMax_N = CastMapY - 4;
                    spawn.WanderMax_S = CastMapY + 4;
                    spawn.WanderMax_W = CastMapX - 4;
                    spawn.Water = true;
                    spawn.Wisdom = 5;

                    spawn.LootItems = corpse.LootToDrop;

                    //spawn.CreatureGroup_Active = creatureGroup;
                    //creatureGroup.Creatures.Add(spawn);

                    spawn.SetPosition(CastMapItem);
                    CastMapItem.CreaturesBases.Add(spawn);

                    //creatureGroup.Timer_CreatureRound.Start();
                    spawn.creatureAI.Timer_CreatureRound.Start();

                    updateMapItem = true;
                    corpseSpawned = true;
                }
                if (TimerCounter > 2)
                {
                    CastMapItem.SpellLayer0.Remove(this);
                    updateMapItem = true;
                }
                else
                {
                    spellTimer.Start();
                }
                if (updateMapItem == true)
                {
                    foreach (Character character in CharactersWithin8)
                    {
                        character.AddMapItem(CastMapItem);

                        if (corpseSpawned == true)
                            character.PlaySound("SpawnCorpse", CastMapX, CastMapY);
                    }
                }
            }
        }
    }
}
